﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using yzrilyzr.FAQ.client;
using yzrilyzr.FAQ.protocol;
using yzrilyzr.FAQ.utils;
using yzrilyzr.util;

namespace FAQ_UI
{
	/// <summary>
	/// StickerPage.xaml 的交互逻辑
	/// </summary>
	public partial class StickerPage : Page, MsgPackReceiver
	{
		private List<string> stickerPacks = new List<string>();
		private List<long> stickers = new List<long>();
		private StickerHelper helper;
		private FAQID to;

		public StickerPage()
		{
			InitializeComponent();
			FAQUtil.eventBus.addReceiver(this);
			DataContext = this;
			ThisPage.Background = Theme.BackBrush;
			helper = FAQUtil.client.getStickerHelper();
			Page_Loaded(null, null);
			updateSelect();
			FAQUtil.eventBus.addReceiver(this);
			FAQUtil.client.addReceiver(this);
		}

		private void updateSelect()
		{
			string[] ps = helper.getStickerPack();
			stickerPacks.Clear();
			stickerPacks.AddRange(ps);
			StickerSelectList.notifyDataChanged();
			helper.setCurrentSticker(StickerHelper.FAVOURITE);
			updateSticker();
		}

		private void updateSticker()
		{
			java.lang.Long[] ss = helper.getStickers();
			stickers.Clear();
			foreach (var sticker in ss)
			{
				stickers.Add(sticker.longValue());
			}
			stickers.Sort((p1, p2) =>
			{
				if (p1 == p2) return 0;
				return p1 > p2 ? -1 : 1;
			});
			StickerGrid.notifyDataInvalidated();
		}

		public List<string> BStickerSelectDataSource
		{ get { return stickerPacks; } }

		public Holder BStickerHolder
		{ get { return new Holder(helper); } }

		public List<long> BStickerDataSource
		{ get { return stickers; } }

		public Holder1 BStickerSelectHolder
		{ get { return new Holder1(helper); } }

		public void onItemClick(object listview, OnItemClickEvent e)
		{
			if (listview == StickerSelectList)
			{
				string key = stickerPacks[e.index];
				helper.setCurrentSticker(key);
				updateSticker();
			}
			else if (listview == StickerGrid)
			{
				long key = stickers[e.index];
				byte[] ic = helper.get(key);
				FAQUtil.client.sendPic(to, ic, true, false);
				FAQUtil.eventBus.asyncSend(new BinMsgPack(App.CLOSE_STICKER));
			}
		}

		public void receive(MsgPackTransfer mpt, MsgPack mp)
		{
			int cmd = mp.getCmd();
			if (cmd == App.OPEN_STICKER_TO)
			{
				this.to = mp.getTo();
				Logger.getDef().i("ChaStickertTo", to);
			}
			else if (cmd == App.UPDATE_STICKER)
			{
				Dispatcher.Invoke(new Action(() =>
				{
					Logger.getDef().i("updateSticker");
					updateSticker();
				}));
			}
		}

		public class Holder : ViewHolder
		{
			private StickerHelper helper;
			public MyDrawableView icon;

			public override ViewHolder newInstance()
			{
				return new Holder(helper);
			}

			public Holder(StickerHelper helper)
			{
				this.helper = helper;
				icon = new MyDrawableView();
				icon.Width = 80;
				icon.Height = 80;
				icon.Stretch = Stretch.Uniform;
				setView(icon);
			}

			public override void set(int i, object data, IList datalist)
			{
				icon.Drawable = FAQUtil.imageCache.get("chatpic" + data, new Loader<Drawable>(() =>
				{
					long key = (long)data;
					byte[] ic = helper.get(key);
					return FAQUtil.getDrawable(ic);
				}));
			}
		}

		public class Holder1 : ViewHolder
		{
			private StickerHelper helper;
			public MyDrawableView icon;

			public override ViewHolder newInstance()
			{
				return new Holder1(helper);
			}

			public Holder1(StickerHelper helper)
			{
				this.helper = helper;
				icon = new MyDrawableView();
				icon.Width = 40;
				icon.Height = 40;
				icon.Stretch = Stretch.Fill;
				setView(icon);
			}

			public override void set(int i, object data, IList datalist)
			{
				icon.Drawable = FAQUtil.imageCache.get("sticker_icon" + data, new Loader<Drawable>(() =>
				{
					string key = (string)data;
					if (key == StickerHelper.FAVOURITE)
					{
						return FAQUtil.getDrawableRes("/assets/favourite.png");
					}
					else if (key == StickerHelper.DEFAULT)
					{
						return FAQUtil.getDrawableRes("/assets/face.png");
					}
					byte[] ic = helper.getIcon(key);
					return FAQUtil.getDrawable(ic);
				}));
			}
		}

		private void Page_Unloaded(object sender, RoutedEventArgs e)
		{
			//FAQUtil.eventBus.removeReceiver(this);
			//FAQUtil.client.removeReceiver(this);
		}

		private void Page_Loaded(object sender, RoutedEventArgs e)
		{
			FAQUtil.eventBus.addReceiver(this);
			FAQUtil.client.addReceiver(this);
		}
	}
}